__CONFIG_SH_NO_DOTDOT=false
__CONFIG_SH_NO_VARIABLE=false
__CONFIG_SH_CONFIG_FOLDER=$HOME/config_files

function __config_sh_usage() {
	echo -e "config - Version: [\x1b[33;1m0.0.1\x1b[m]"
	echo -e "------------------------------------------------------------"
	echo -e "    \x1b[1m--get\x1b[m     :Gets values"
__CONFIG_SH_CONFIG_FOLDER=/home/jan/config/
	echo -e "    \x1b[1m--set\x1b[m     :Sets values"
	echo -e "    \x1b[1m--remote\x1b[m  :Values to get from the Config Server"
	echo -e "    \x1b[1m--local\x1b[m   :Values to get from local config files"
	echo -e "Format:"
	echo -e "    \x1b[1m--no-dotdot\x1b[m  :"
	echo -e "    \x1b[1m--no-variable\x1b[m:"
}

function __config_sh_dotdot() {
	${__CONFIG_SH_NO_DOTDOT} && return
	echo ':: '$@
}

function __config_sh() {
	STORE_CONFIG_SERVER="http://se1-iva-p101.sys.schwarz/config/storeserver.php"
	ROOT="$(dirname $(readlink -f $0))"
	METHOD=""
	LOCAL=""
	REMOTE=""

	while [ $# -gt 0 ]; do
		case "${1}" in
			'--no-dotdot'|'-nd') __CONFIG_SH_NO_DOTDOT=true; ;;
			'--no-variable'|'-nv') __CONFIG_SH_NO_VARIABLE=true; ;;
			'--help'|'-?'|'-h') __config_sh_usage; ;;
			'--get') METHOD="get" ;;
			'--set') METHOD="set" ;;
			'-l'|'-r')
				case "${1}" in
					'-l') STORE_TO='LOCAL' ;;
					'-r') STORE_TO='REMOTE' ;;
				esac
			;&
			'--remote'|'--local')
				[ -z "${STORE_TO}" ] && STORE_TO="$(echo ${1} | cut -c3- | tr '[[:lower:]]' '[[:upper:]]')"

				shift
				while [ $# -gt 1 ] && ! [ "$(echo $2 | cut -c-1)" = "-" ]; do
					eval "${STORE_TO}=\"\$${STORE_TO} ${1}\""
					shift
				done
				eval "${STORE_TO}=\"\$${STORE_TO} ${1}\""
			;;
			*)
				__config_sh_usage
				exit
			;;
		esac
		shift
	done

	if ! [ "${LOCAL}" = "" ]; then
		__config_sh_${METHOD}_locals
	fi

	if ! [ "${REMOTE}" = "" ]; then
		STORE_CONFIG_SERVER=$(__config_sh_get_config_server < ${__CONFIG_SH_CONFIG_FOLDER}/servers )
		[ $? -ne 0 ] && { exit 1; }
		__config_sh_get_remotes | \
		if $__CONFIG_SH_NO_VARIABLE; then
			sed "s/^.*='//; s/'$//"
		fi
	fi
}

function __config_sh_get_locals() {
	__config_sh_dotdot 'local'
	for i in $LOCAL; do
		if [ -f ${__CONFIG_SH_CONFIG_FOLDER}/locals/$i'.get' ]; then
			echo $i"='"$(/bin/bash ${__CONFIG_SH_CONFIG_FOLDER}/locals/$i'.get')"'" | awk -v dotdot=$__CONFIG_SH_NO_DOTDOT 'dotdot == "true" { print "local_" $0; next} { print $0 }' \
				| if $__CONFIG_SH_NO_VARIABLE; then
					sed "s/^.*='//; s/'$//"
				else
					cat -
				fi
		else
			__config_sh_dotdot "not found: $i"
		fi
	done
}

function __config_sh_set_locals() {
	for i in $LOCAL; do
		name=$(echo $i | cut -f1 -d=)
		if [ -f ${__CONFIG_SH_CONFIG_FOLDER}/locals/$name'.set' ]; then
			/bin/bash ${__CONFIG_SH_CONFIG_FOLDER}/locals/$name'.set' "$(echo $i | cut -f2- -d'=')"
		else
			__config_sh_dotdot "read-only: $name"
		fi
	done
}

function __config_sh_get_remotes() {
	__config_sh_dotdot "remote"
	# TODO: make this more dynamic
	# FIXME: Disallow overwrite
	QUERY_NAME=""
	VARNAME=""
	T=$(mktemp)
	while read -r line; do
		echo -ne '[....] store_server_name == '$line'?\r' 1>&2
		eval $line
		VARNAME=$(echo $line | cut -f1 -d'=')
		curl -s -F format=set -F ${VARNAME}=${!VARNAME} -F action=LIST_STORE ${STORE_CONFIG_SERVER} > ${T}
		if [ $? -eq 0 ]; then
			echo -e '[\x1b[32;1m ok \x1b[m]' 1>&2
			QUERY_NAME=$line
			break
		else
			echo -e '[\x1b[31;1mfail\x1b[m]' 1>&2
		fi
	done < ${__CONFIG_SH_CONFIG_FOLDER}/remote_try

	for i in $REMOTE; do
		grep -iE $i ${T} | sed -E 's/set .*\[[0-9]+\]=//'
	done
	rm ${T}
}


function __config_sh_get_config_server() {
	ping -q -c3 -i1 $(echo ${STORE_CONFIG_SERVER} | sed -e 's|^[^/]*//||' -e 's|/.*$||') -w3 >/dev/null 2>&1
	if [ $? -eq 0 ]; then
		echo $STORE_CONFIG_SERVER
		return 0
	fi

	T=$(mktemp)
	cat - > ${T}
	sed -e 's|^[^/]*//||' -e 's|/.*$||' ${T} | uniq | while read -r line; do
		echo -ne '[....] trying: '$(echo $line)'\r' 1>&2
		ping -q -c3 -i1 $line -w3 > /dev/null 2>&1
		if [ $? -eq 0 ]; then
			STORE_CONFIG_SERVER="http://$(grep -E -m1 $line ${T})"
			sed -Ei "0,/[[:space:]]*STORE_CONFIG_SERVER/ s![[:space:]]*STORE_CONFIG_SERVER=.*!\tSTORE_CONFIG_SERVER=\"$STORE_CONFIG_SERVER\"!" $(readlink -f $0)
			echo -e '[\x1b[32;1m ok \x1b[m]' 1>&2
			echo ${STORE_CONFIG_SERVER}
			exit
		else
			echo -e '[\x1b[31;1mfail\x1b[m]' 1>&2
		fi
	done
	rm ${T}

	__config_sh_dotdot "no server found" 1>&2
	return 1
}

# ENTRYPOINT
__config_sh $@
